<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" />江苏传智播客教育科技股份有限公司<span />
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 树形控件 -->
        <el-tree :data="List" default-expand-all>
          <!-- 	自定义树节点的内容，参数为 { node, data } -->
          <template #default="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item>添加子部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <!-- 关闭遮罩层 -->
      <!-- 关闭点击esc实现退出功能 -->
      <el-dialog
        title="新增或编辑"
        :visible.sync="showDialog"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
      ><addOrEdit :id="curId" /></el-dialog>
      <!-- :id="curId" 将父组件部门的id传到子组件作为子部门的pid -->
    </div>
  </div>
</template>

<script>
// 查询企业部门列表
import { getDepartment } from '@/api/departments'
import { transDataToTreeData } from '@/utils' // 引入树形结构函数
import addOrEdit from './deptDialog.vue'
export default {
  components: {
    addOrEdit
  },
  data() {
    return {
      List: [], // 存储企业部门列表
      showDialog: false,
      curId: '' // 当前点击部门的id
    }
  },
  created() {
    this.loadDepartment()
  },
  methods: {
    // 加载数据
    async loadDepartment() {
      try {
        const res = await getDepartment()
        console.log(res)
        console.log('getDepartment', res)
        res.data.depts.shift() // 删除第一条数据
        // this.List = res.data.depts
        this.List = transDataToTreeData(res.data.depts)
      } catch (error) {
        // console.log('啊啊啊啊啊啊')
        console.log('1111', error)
      }
    },
    hAdd(id) {
      this.curId = id
      this.showDialog = true
    }
  }
}
</script>
